package com.cndym.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil {
    public static String replace(String strSource, String strFrom, String strTo) {
        if (strSource == null) {
            return null;
        }
        int i = 0;
        if ((i = strSource.indexOf(strFrom, i)) >= 0) {
            char[] cSrc = strSource.toCharArray();
            char[] cTo = strTo.toCharArray();
            int len = strFrom.length();
            StringBuffer buf = new StringBuffer(cSrc.length);
            buf.append(cSrc, 0, i).append(cTo);
            i += len;
            int j = i;
            while ((i = strSource.indexOf(strFrom, i)) > 0) {
                buf.append(cSrc, j, i - j).append(cTo);
                i += len;
                j = i;
            }
            buf.append(cSrc, j, cSrc.length - j);
            return buf.toString();
        }
        return strSource;
    }


    public static String getXml(String expect, String lottery_number) {
        String str = "";
        lottery_number = replace(lottery_number, "+", ",");
        str += "<?xml version='1.0' encoding='UTF-8'?>\n";
        str += "<root>\n";
        str += "<open_expect>" + expect + "</open_expect>\n";
        str += "<open_number>\n";
        String[] ss = lottery_number.split(",");
        for (int i = 0; i < ss.length; i++) {
            str += "\t<num" + (i + 1) + ">" + ss[i] + "</num" + (i + 1) + ">\n";
        }
        str += "</open_number>\n";
        str += "</root>";
        return str;
    }


    public static boolean http_get(String url, byte[] content, OutputStream out, String[] hd) {
        try {
            URLConnection uc = new URL(url).openConnection();
            uc.setConnectTimeout(180000);
            uc.setReadTimeout(150000);
            if (hd != null) {
                for (int i = 0; i < hd.length - 1; i += 2) {
                    uc.addRequestProperty(hd[i], hd[i + 1]);
                }
            }

            if (content != null)
                uc.setDoOutput(true);
            uc.connect();

            if (content != null) {
                OutputStream uos = uc.getOutputStream();
                uos.write(content);
                uos.flush();
                uos.close();
            }

            int cl = uc.getContentLength();

            InputStream is = uc.getInputStream();
            byte[] bb = new byte[1024];
            int n = 0, nn = 0;

            while ((n = is.read(bb)) >= 0) {
                nn += n;
                out.write(bb, 0, n);
            }
            is.close();

            return (cl <= 0 || cl == nn);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return false;
    }


    public static boolean http_get(String url, byte[] content, File fn) {
        return http_get(url, content, fn, null);
    }

    public static boolean http_get(String url, byte[] content, File fn, String[] hd) {
        try {
            OutputStream out = new FileOutputStream(fn);
            boolean rr = http_get(url, content, out, hd);
            out.close();
            return rr;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return false;
    }


    public static byte[] http_get(String url, byte[] content, String[] hd) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (http_get(url, content, out, hd))
            return out.toByteArray();
        return null;
    }


    public static String http_get(String url, String content, String enc, String[] hd) {
        try {
            byte[] rbytes = http_get(url, content != null ? content.getBytes(enc) : null, hd);
            if (rbytes != null)
                return new String(rbytes, enc);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static String http_get(String url, String content, String enc) {
        return http_get(url, content, enc, null);
    }


    public static String http_get(String url, String content) {
        return http_get(url, content, "utf-8");
    }


    public static String http_get(String url) {
        return http_get(url, null, "utf-8");
    }

    public static int getContentLength(String url) {
        try {
            URLConnection uc = new URL(url).openConnection();
            uc.connect();
            return uc.getContentLength();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return -1;
    }

    public static void main(String[] args) throws Exception {
        String r = http_get("http://www.baidu.com/", null, "gbk");
        System.out.println(r);
    }

}
